package com.weipeep.sys.controller;


import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weipeep.common.config.Constant;
import com.weipeep.sys.domain.DeptDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weipeep.sys.domain.CompanyDO;
import com.weipeep.sys.service.CompanyService;
import com.weipeep.common.utils.Result;

/**
 * 
 * <pre>
 * 公司管理
 * </pre>
 * <small> 2018-08-06 12:08:39 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/company")
public class CompanyController {
	@Autowired
	private CompanyService companyService;
	
	@GetMapping()
	@RequiresPermissions("sys:company:company")
	String Company(){
	    return "sys/company/company";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:company:company")
	public Result<Page<CompanyDO>> list(Integer pageNumber, Integer pageSize, CompanyDO companyDTO){
		// 查询列表数据
        Page<CompanyDO> page = new Page<>(pageNumber, pageSize);
        Wrapper<CompanyDO> wrapper = new QueryWrapper<CompanyDO>(companyDTO);
        page = (Page<CompanyDO>) companyService.page(page, wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add/{parentId}")
	@RequiresPermissions("sys:company:add")
	String add(@PathVariable("parentId") Long parentId,Model model){
		model.addAttribute("parentId", parentId);
		if (parentId == 0) {
			model.addAttribute("parentName", "无");
		} else {
			model.addAttribute("parentName", companyService.getById(parentId).getCompanyName());
		}
	    return "sys/company/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("sys:company:edit")
	String edit(@PathVariable("id") Long id,Model model){
		CompanyDO company = companyService.getById(id);
		model.addAttribute("company", company);
		if (Constant.Sys.COMPANY_ROOT_ID.equals(company.getParentId())) {
			CompanyDO companyDO = new CompanyDO();companyDO.setId(0L);companyDO.setCompanyName("无");
			model.addAttribute("parentCompany", companyDO);
		} else {
			CompanyDO parentCompany = companyService.getById(company.getParentId());
			model.addAttribute("parentCompany", parentCompany);
		}
	    return "sys/company/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:company:add")
	public Result<String> save( CompanyDO company){
		if(companyService.save(company)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sys:company:edit")
	public Result<String>  update( CompanyDO company){
		companyService.updateById(company);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("sys:company:remove")
	public Result<String>  remove( Long id){
		if(companyService.removeById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("sys:company:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		companyService.removeByIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
